package com.hl.sfzy.controller;

import com.hl.sfzy.constant.CommonConstant;
import com.hl.sfzy.constant.SystemCode;
import com.hl.sfzy.entity.Course;
import com.hl.sfzy.entity.User;
import com.hl.sfzy.service.UserService;
import com.hl.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @RestController : @ResponseBody + @Controller
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService UserService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody User User) throws Exception {
        UserService.add(User);
        return Result.success();
    }

    /**
     * 编辑
     */
    @PutMapping("/edit")
    public Result edit(@RequestBody User User) {
        System.out.println(User);
        UserService.edit(User);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Integer id) {
        UserService.remove(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/remove")
    public Result removeBatch(Integer[] ids) {
        System.out.println(ids);
        UserService.removeBatch(ids);
        return Result.success();
    }

    /**
     * 单条查询
     */
    @GetMapping("/find/{id}")
    public Result findById(@PathVariable Integer id) {
        return Result.success(UserService.findById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/find")
    public Result findPage(@RequestParam(value = CommonConstant.PAGE_NUM, required = false, defaultValue = CommonConstant.PAGE_NUM_DEFAULT) Integer pageNum,
                           @RequestParam(value = CommonConstant.PAGE_SIZE, required = false, defaultValue = CommonConstant.PAGE_SIZE_DEFAULT) Integer pageSize,
                           User User) {
        return Result.success(UserService.findPage(pageNum, pageSize, User));
    }

    /**
     * 判断用户名是否重复
     */
    @GetMapping("/findExists/{username}")
    public Result findUsernameExists(@PathVariable String username) {
        User User = UserService.findByUsername(username);
        if (User == null) {
            return Result.success();
        }
        return Result.error(SystemCode.USERNAME_EXISTS.getCode(), SystemCode.USERNAME_EXISTS.getMessage(), null);
    }

    /**
     * 登录
     * @param user
     * @return
     */
    @GetMapping("/login")
    public Result login( User user){
        User userInfo = UserService.login(user);
        if (userInfo == null) {
            return Result.error("账号或密码错误！");
        }
        return Result.success(userInfo);
    }
    /**
     * 小程序登录
     * @param user
     * @return
     */
    @GetMapping("/userLogin")
    public Result userLogin( User user){
        User userInfo = UserService.userLogin(user);
        if (userInfo == null) {
            return Result.error("账号或密码错误！");
        }
        return Result.success(userInfo);
    }
    /**
     * 课程
     * @param id
     * @return
     */
    @GetMapping("/getUserCourses")
    public  Result getUserCourses(String id){
        List<Course> userCourses = UserService.getUserCourses(id);
        if (userCourses.size() == 0){
            return Result.error("该用户没有课程！");
        }
        return Result.success(userCourses);
    }

    @GetMapping("/getUserCollections")
    public  Result getUserCollections(String id){
        List<Course> userCollections = UserService.getUserCollections(id);
        if (userCollections.size() == 0){
            return Result.error("该用户没有课程！");
        }
        return Result.success(userCollections);
    }
}